package com.zw.mes.module.shipments.controller;


import com.zw.mes.base.BaseController;
import com.zw.mes.common.JsonResult;
import com.zw.mes.entity.shipments.InstDelivery;
import com.zw.mes.entity.shipments.InstShipmentsDetail;
import com.zw.mes.entity.shipments.InstShipmentsList;
import com.zw.mes.entity.shipments.InstShipmentsPlanSheet;
import com.zw.mes.module.assemble.entity.AsseAssemblySheet;
import com.zw.mes.module.assemble.entity.AsseStore;
import com.zw.mes.module.assemble.repository.AsseAssemblySheetRepository;
import com.zw.mes.module.assemble.service.AssMakeService;
import com.zw.mes.module.assemble.service.DispatchService;
import com.zw.mes.module.cost.service.DictCommonService;
import com.zw.mes.module.shipments.model.LinshiProductDetail;
import com.zw.mes.module.shipments.repository.InstShipmentsRepository;
import com.zw.mes.module.shipments.repository.PlanSheetRepository;
import com.zw.mes.module.shipments.service.InstDeliveryService;
import com.zw.mes.module.shipments.service.InstShipmentsPlanService;
import com.zw.mes.module.shipments.service.InstShipmentsService;
import com.zw.mes.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * @Description: 出库单
 * <p>
 * 权限：
 * @Author: liqingyu
 * @Date: 2020/11/10 08:58
 */
@Controller
@RequestMapping("/delivery")
@SuppressWarnings("all")
public class InstDeliveryController extends BaseController {


    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private InstShipmentsPlanService instShipmentsPlanService;
    @Autowired
    private PlanSheetRepository planSheetRepository;
    @Autowired
    private InstDeliveryService instDeliveryService;
    @Autowired
    private InstShipmentsRepository instShipmentsRepository;
    @Autowired
    private AssMakeService makeService;
    @Autowired
    private AsseAssemblySheetRepository assemblySheetRepository;
    @Autowired
    private DispatchService dispatchService;

    /**
     * 跳转创建页面
     * @param model
     * @return
     */
    @GetMapping("/create")
    public String create(Model model) {
        model.addAttribute("createName", ShiroUtils.getUser().getName());
        model.addAttribute("createTime", new Date());
        model.addAttribute("dept", ShiroUtils.getUser().getDeptName());
        return "shipments/Delivery/pageCreate";
    }
    /**
     * 跳转提交页面
     * @param model
     * @return
     */
    @GetMapping("/submit")
    public String submit(Model model) {
        return "shipments/Delivery/pageSubmit";
    }
    /**
     * 跳转选择制作件页面
     */
    @GetMapping("/addProduct")
//    @RequiresPermissions({"dsn:proBull:view"})
    public String tzrwList(Model model) {
        return "shipments/Delivery/addProductPage";
    }
    /**
     * 跳转提交页面
     * @param model
     * @return
     */
    @GetMapping("/query")
    public String query(Model model) {
        return "shipments/Delivery/pageQuery";
    }
    /**
     * 查询成品库记录
     */
    @ResponseBody
    @PostMapping("/productDetailList")
    public JsonResult productDetailList(AsseStore asseStore) {
        startPage();
        List<AsseStore> list = instDeliveryService.selectLinshiProductDetail(asseStore);
        return jsonResult(list);
    }
    /**
     * 提交页面
     */
    @ResponseBody
    @PostMapping("/submitList")
    public JsonResult submitList(InstDelivery instDelivery) {
        startPage();
        List<InstDelivery> list = instDeliveryService.selectInstDelivery(instDelivery);
        return jsonResult(list);
    }
    /**
     * 创建出库单保存
     */
    @ResponseBody
    @PostMapping("/saveDelivery")
    public JsonResult saveDelivery(InstDelivery instDelivery,String[] prodNo,String[] proName,String[] drawNo,String[] drawName,String[] drawNum,String[] comment,String[] asseId,String[] sheetId) {
        int list = instDeliveryService.saveDelivery(instDelivery,prodNo,proName,drawNo,drawName,drawNum,comment,asseId,sheetId);
        return jsonResult(list);
    }
    /**
     * 创建出库单保存
     */
    @ResponseBody
    @PostMapping("/del")
    public JsonResult del(Integer id) {
        int list = instDeliveryService.del(id);
        return jsonResult(list);
    }
    /**
     * @Author wsg
     * @Date 12:55 2021/5/7
     * @Description 领料计划查看 页面
     **/
    @GetMapping("/makeView/{sheetId}")
    public String makeView(@PathVariable("sheetId") Integer sheetId, Model model) {
        AsseAssemblySheet asse = assemblySheetRepository.getOne(sheetId);
        model.addAttribute("asse", asse);
        model.addAttribute("details", dispatchService.findAssemblyDetailBySheetId(sheetId));
        model.addAttribute("buyList", makeService.findAsseAssemblyBuyList(sheetId));
        String path = "";
        if (asse.getSpecial().equals("自动化")) {
            path = "assemble/make/makeAmsView";
        } else {
            path = "assemble/make/makeView";
        }
        return path;
    }
}
